/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package filetransserv;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JFormattedTextField;
import javax.swing.JTextField;

/**
 *
 * @author CarlosScipione
 */
public class Client {

    private Socket socket;

    public Client(String Ip) throws IOException, Exception {

        try {

            //
            // Create a connection to the server socket on the server application
            //

            if (validateIP(Ip)) {

                byte[] b = new byte[4];
                b[0] = (byte) (Integer.parseInt(Ip.substring(0, Ip.indexOf("."))));

                Ip = Ip.substring(Ip.indexOf(".") + 1, Ip.length());
                b[1] = (byte) (Integer.parseInt(Ip.substring(0, Ip.indexOf("."))));

                Ip = Ip.substring(Ip.indexOf(".") + 1, Ip.length());
                b[2] = (byte) (Integer.parseInt(Ip.substring(0, Ip.indexOf("."))));

                Ip = Ip.substring(Ip.indexOf(".") + 1, Ip.length());
                b[3] = (byte) (Integer.parseInt(Ip));


                InetAddress host = InetAddress.getByAddress(b);
                //String ip = host.getHostAddress();

                if (host.isReachable(2500)) {
                    socket = new Socket(host, 7777);
                } else {
                    throw new Exception("Host Inalcanzable.");
                }
            } else {
                throw new Exception("IP Invalida.");
            }


        } catch (UnknownHostException e) {
            throw e;
        } catch (IOException e) {
            throw new Exception("Error en la comunicacion.");
        } catch (NumberFormatException e) {
            throw new Exception("Campo de IP vacio.");
        }
    }

    Client() {
    }

    private static boolean validateIP(String IP) {
        int value = 0;

        for (int i = 0; i < 4; i++) {
            if (i == 3) {
                value = Integer.parseInt(IP);
            } else {
                value = Integer.parseInt(IP.substring(0, IP.indexOf(".")));
                IP = IP.substring(IP.indexOf(".") + 1, IP.length());
            }
            if ((value < 0) || (value > 255)) {
                return false;
            }

        }

        return true;
    }

    public void notificationDownload(String bytes, String IP) throws
            UnknownHostException, Exception {

        try {
            ObjectOutputStream oos = new ObjectOutputStream(
                    socket.getOutputStream());
            oos.writeObject("notificationDownload:" + bytes + ":" + IP);

            oos.close();

        } catch (UnknownHostException e) {
            throw e;
        } catch (IOException e) {
            throw new Exception("Problema para acceder a NOTIFICACIONES.");
        } catch (Exception e) {
            throw new Exception("Problema para acceder a NOTIFICACIONES.");
        }
    }

    public void notificationUpload(String bytes, String IP) throws
            UnknownHostException, Exception {

        try {
            ObjectOutputStream oos = new ObjectOutputStream(
                    socket.getOutputStream());
            oos.writeObject("notificationUpload:" + bytes + ":" + IP);

            oos.close();

        } catch (UnknownHostException e) {
            throw e;
        } catch (IOException e) {
            throw new Exception("Problema para acceder a NOTIFICACIONES.");
        } catch (Exception e) {
            throw new Exception("Problema para acceder a NOTIFICACIONES.");
        }
    }
}
